import java.util.ArrayList;
import java.util.LinkedList;

// 1、无头单向非循环链表实现
public class SingleLinkedList {
     class ListNode{
        int val;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    private ListNode head;

    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);

        if(head == null){
            head = node;
            return;
        }

        ListNode cur = head;
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = node;

    }

    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data){
        //首选判断index合法性
        if(index < 0 || index > size()){
//            throw new IndexOutOfBoundException("索引非法");
            System.out.println("索引非法");
            return false;
        }

        if(index == 0){
            addFirst(data);
            return true;
        }

//        if(index == size()){
//            addLast(data);
//            return true;
//        }

        ListNode node = new ListNode(data);
        int count = 0;
        ListNode cur = head;
        while(count != index - 1){
            count++;
            cur = cur.next;
        }
        node.next = cur.next;
        cur.next = node;
        return true;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = head;

        if(head == null){
            return;
        }

        if(head.val == key){
            head = head.next;
            return;
        }

        while(cur.next != null){
            if(cur.next.val == key){
                ListNode next = cur.next;
                cur.next = next.next;
                return;
            }
            cur = cur.next;
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key){

        while(head != null && head.val == key){
            head = head.next;
        }

        ListNode cur = head;

        while(cur != null && cur.next != null){
            ListNode next = cur.next;
            if(next.val == key){
                while(next != null && next.val == key){
                    next = next.next;
                }
                cur.next = next;
            }
            cur = cur.next;
        }
    }

    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNode cur = head;
        while(cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }

    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void clear(){
        head = null;
    }

    public static void main(String[] args) {
        SingleLinkedList list = new SingleLinkedList();
//        list.addLast(1);
//        list.addLast(2);
//        list.addLast(3);
//        list.addLast(4);
//        list.display();
//
//        list.addIndex(0,0);
//        list.display();
//
//        list.remove(0);
//        list.display();

        list.addLast(2);
        list.addLast(2);
        list.addLast(3);
        list.addLast(4);
        list.addLast(2);
        list.display();
        list.removeAllKey(2);
        list.display();

    }
}